import sys

from liwe.module import LiweModule

class Comment ( LiweModule ):

	def __init__ ( self, liwe ):
		super ( Comment, self ).__init__ ( liwe, "comment" )

		self.dbm = self.liwe.dbm

	def add_comment ( self, id_obj, module, txt, id_site = 0 ):
		"""
		add_comment ( id_obj, module, txt, id_site )

		INPUT:
			- id_obj	- Object ID the comment refers to
			- module	- The module name the id_obj refers to
			- txt		- Comment text
			- id_site	- ID Site

		RETURN: 
			- id_comment
		"""

		id_page = self.liwe.invoke ( "page_manager.admin.create", text = txt, module = self.name ) [ 'id_page' ]

		dct = {
			"id_obj" : id_obj,
			"module" : module,
			"id_site" : id_site,
			"id_page" : id_page,
			"uid" : self.liwe.pyhp.session.data.get ( "uid" ),
			"visible" : self.cfg.get ( "visible", 1 )
		}

		self.dbm.insert ( "obj_comment", dct )
		id_comment = self.dbm.insert_id ()

		return id_comment

	def del_comment ( self, id_comment = 0, ids = "" ):
		
		if id_comment: 
			self._del_comment ( id_comment )
			return

		id_lst = ids.split ( '|' )
		for id in id_lst:
			self._del_comment ( id )
			sys.stderr.write ( " ----- obj id: %s\n" % id )

	def _del_comment ( self, id_comment ):
		"""
		_del_comment ( id_comment )

		INPUT:
			- id_comment	- ID of the comment to be deleted

		RETURN:
			NONE
		"""

		# Recupero l'id page dato ``id_comment``
		vals = self.dbm.single_row ( "obj_comment", "id_page", "id = '%s'" % id_comment )
		if not vals:
			return

		id_page = vals [ 'id_page' ]
		
		self.liwe.invoke ( "page_manager.admin.delete", id_page = id_page )
		self.dbm.delete ( "obj_comment", "id = '%s'" % id_comment )
		

	def del_by_id_obj ( self, id_obj, module, id_site = 0 ):
		"""
		del_by_id_obj ( id_obj, module, id_site = 0 )

		Deletes all comments related to ``id_obj``

		INPUT:
			- id_obj
			- module
			- id_site
		"""
		conds = []
		conds.append ( "id_obj = '%s'" % id_obj )
		conds.append ( "module = '%s'" % module )
		conds.append ( "id_site = '%s'" % id_site )
		
		rows = self.dbm.select ( "obj_comment", "id", " AND ".join ( conds ) )
		if not rows: return

		for row in rows:
			self._del_comment ( row [ 'id' ] )

	def list ( self, id_obj = "", module = "", order_by = "", id_site = 0 , start_point = 0, do_count = 0 ):
		"""
		list ( id_obj, module, id_site, order_by, start_point, do_count )

		Lists all comments related to ``id_obj``.

		"""

		conds = []

		if id_obj : conds.append ( "c.id_obj = '%s'" % id_obj )
		conds.append ( "c.module = '%s'" % module )
		conds.append ( "c.id_site = '%s'" % id_site )

		conds.append ( "p.uid = u.uid" )
		conds.append ( "c.id_page = p.id_page" )

		# the default value for 'extras' is the one mostly used ,
		# the 'order_by' option is primarily used for amministration purposes.
		extras = "ORDER BY c.id DESC"
		if order_by:
			rel = { 'uid' : 'c.uid', 'page' : 'c.id_obj', 'create' : 'p.created' } 
			extras =  "ORDER BY %s DESC" % rel.get ( order_by, 'c.id' )	

		fields = [ 
			  "c.id", "c.module", "c.id_obj", 
			  "p.txt" , "p.uid", "p.created",
			  "u.login"
			 ]

		tot_rows = 0
		if do_count:
			tot_rows = self.dbm.count ( [ "obj_comment AS c", "page AS p", "user_login AS u" ], fields, " AND ".join ( conds ) )
		#self.dbm.debug = sys.stderr.write
		rows = self.dbm.all_rows ( [ "obj_comment AS c", "page AS p", "user_login AS u" ], fields, " AND ".join ( conds ),
						extras = extras )
		#self.dbm.debug = None
		if not rows: rows = []
		return { "rows" : rows, "tot_rows" : tot_rows }

	def get_modules ( self, id_site ):
		"""
		get_modules ( id_site )

		get all commented modules 
		"""
		
		res = self.dbm.all_rows ( "obj_comment", "module", conds = "", extras = "GROUP BY module" )
		return res
	
